﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;

namespace ControlCenter.通讯
{
    /// <summary>
    /// 包装服务器端套接字
    /// </summary>
    public class TcpServerSocket
    {
        #region ===自定义常量===

        /// <summary>
        /// 缺省缓冲区大小
        /// </summary>
        private const int DEFAULT_BUFFER_SIZE = 1024*12;

        #endregion

        #region ===私有字段===

        /// <summary>
        /// 套接字
        /// </summary>
        private Socket _socket;

        /// <summary>
        /// 用户登录ID
        /// </summary>
        private string ID;

        /// <summary>
        /// 数据缓冲区
        /// </summary>
        private byte[] _buffer;



        private string[] RIDs;

        #endregion


        #region ===公有属性===

        /// <summary>
        /// 数据缓冲区
        /// </summary>
        public byte[] Buffer
        {
            get
            {
                return _buffer;
            }
        }

        /// <summary>
        /// 套接字
        /// </summary>
        public Socket Socket
        {
            get
            {
                return _socket;
            }
        }

        /// <summary>
        /// 此连接管理的线路ID
        /// </summary>
        public string[] RouteIDs
        {
            get
            {
                return RIDs;
            }
            set
            {
                RIDs = value;
            }
        }

        /// <summary>
        /// 用户数据
        /// </summary>
        public string UserData
        {
            get
            {
                return ID;
            }
            set
            {
                ID = value;
            }
        }

        /// <summary>
        /// 用户自定义数据对角
        /// </summary>
        public object UserState
        {
            get;
            set;
        }


        #endregion




        public bool HaveThisRouteID(string RouteID)
        {
            bool RValue = false;
            if (null != RouteIDs)
            {
                int i, count = 0;
                count = RouteIDs.Length;
                for (i = 0; i < count; i++)
                {
                    if (RouteID == RouteIDs[i])
                    {
                        RValue = true;
                        break;
                    }
                }
            }

            return RValue;
        }

        #region ===构造函数===

        public TcpServerSocket(Socket socket)
            : this(socket, DEFAULT_BUFFER_SIZE)
        {

        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="socket">要包装的套接字</param>
        /// <param name="buffersize">缓冲区大小</param>
        public TcpServerSocket(Socket socket, int buffersize)
        {

            if (socket == null)
            {
                throw new Exception("TcpServerSocket_Constructor_Error:套接字不能为空");
            }
            if (buffersize <= 0)
            {
                throw new Exception("TcpServerSocket_Constructor_Error:数据缓冲区长度须大于0");
            }

            this._socket = socket;

            this._buffer = new byte[buffersize];

            /////初始化 解协议接收器
            //_ProtocolReceiver = new ICenter3ProtocolReceiver(ProtocolConfigure.Decryptor);
            //_ProtocolReceiver.ToKen = this;
        }

        #endregion
    }
}
